﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.Configuration;
using Microsoft.WindowsAzure.StorageClient;
using System.Data;

namespace MyFloridaHouse.MapServer
{
    public partial class MyBlobStorageReader : System.Web.UI.Page
    {
        public string container;
        public string friendlyTitle;

        protected void Page_Load(object sender, EventArgs e)
        {
            //<img src="http://censusvalidator.blob.core.windows.net/mydistrictbuilder/Banners_files/PublicInfo.jpg" />
            if (Request.QueryString.Count > 0)
            {
                if (Request.QueryString["container"] != null)
                {
                    container = Request.QueryString["container"].ToString();
                }
                else
                {

                }
                if (Request.QueryString["friendlyTitle"] != null)
                {
                    friendlyTitle = Request.QueryString["friendlyTitle"].ToString();
                }
                else
                {
                    friendlyTitle = "Listing of items in the Container";
                }
                if (Request.QueryString["imageUrl"] != null)
                {
                    Image1.ImageUrl = Request.QueryString["imageUrl"].ToString();
                }
                else
                {
                    Image1.ImageUrl = "http://censusvalidator.blob.core.windows.net/mydistrictbuilder/Banners_files/PublicInfo.jpg";
                }
                GetListingsByQueryString();
            }
        }

        protected void GetListingsByQueryString()
        {
            CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
            {
                string connectionString;
                if (RoleEnvironment.IsAvailable)
                {
                    connectionString = RoleEnvironment.GetConfigurationSettingValue(configName);
                }
                else
                {
                    connectionString = ConfigurationManager.AppSettings[configName];
                }
                configSetter(connectionString);
            });


            // Get the storage account reference
            CloudStorageAccount objStorage = CloudStorageAccount.FromConfigurationSetting("Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString");
            // get the Client reference using storage blobend point
            CloudBlobClient objClient = new CloudBlobClient(objStorage.BlobEndpoint, objStorage.Credentials);
            // Get Container reference
            CloudBlobContainer objContainer = objClient.GetContainerReference(container);
            // Browse through blob list from the container
            IEnumerable<IListBlobItem> objBlobList = objContainer.ListBlobs();

            // Create sample data for the DataGrid control.
            DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add(new DataColumn(" ", typeof(string)));
            foreach (IListBlobItem objItem in objBlobList)
            {
                //Response.Write(objItem.Uri + "<br>");
                dr = dt.NewRow();
                string nameString = objItem.Uri.ToString().Substring(objItem.Uri.ToString().LastIndexOf("/") + 1, objItem.Uri.ToString().Length - objItem.Uri.ToString().LastIndexOf("/") - 1);
                dr[0] = "<a href=" + objItem.Uri.ToString().Replace(" ", "%20") + " target=\"new\">" + nameString + "</a>";
                dt.Rows.Add(dr);
                //dataGridItemList.Items("<a href=\"www.ucf.edu\">"+objItem.Uri.ToString()+"</a>");
            }
            DataView dv = new DataView(dt);
            dataGridItemList.DataSource = dv;
            dataGridItemList.DataBind();
            containerLabel.Text = friendlyTitle;
        }
    }
}